// Misperception susceptibility
// Outcome: bias
// Uses outreg2 package
// Used three datasets: PanelPublic.dta, AUC.dta, and tfslantPublic.dta

use "PanelPublic.dta", clear

set scheme uncluttered

egen completeWs=rownonmiss(starttime_W1 starttime_W2 starttime_W3 starttime_W4 starttime_W5 ///
	starttime_W6 starttime_W7 starttime_W8 starttime_W9 starttime_W10 starttime_W11 starttime_W12), strok
tab completeWs

// Missing values
recode gender educ polid_W1 socid_W1 ecoid_W1 pid3 Feel1_W1-TIP4_W1 (8/9=.)

// Demographics
recode gender (1=1) (2=0), gen(maled)
alpha polid_W1 ecoid_W1 socid_W1
egen polid = rowmean(polid_W1 ecoid_W1 socid_W1 )
sum polid
gen age = 2019-birthyr

// Epistemic beliefs
alpha (Feel1_W1-Feel4_W1)
alpha (Evid1_W1-Evid4_W1)
alpha (TIP1_W1-TIP4_W1)
egen feel=rowmean(Feel1_W1-Feel4_W1)
lab var feel "Faith in Intuition for Facts"
egen evid=rowmean(Evid1_W1-Evid4_W1)
lab var evid "Need for Evidence"
egen TIP=rowmean(TIP1_W1-TIP4_W1)
lab var TIP "Truth is Political"
sum feel evid TIP

// Convert to long format
gen id = _n
reshape long starttime_W endtime_W ///
	F1e_W F2e_W F3e_W F4e_W F5e_W F6e_W F7e_W F8e_W F9e_W F10e_W ///
	T1e_W T2e_W T3e_W T4e_W T5e_W T6e_W T7e_W T8e_W T9e_W T10e_W ///
	F1b_W F2b_W F3b_W F4b_W F5b_W F6b_W F7b_W F8b_W F9b_W F10b_W ///
	T1b_W T2b_W T3b_W T4b_W T5b_W T6b_W T7b_W T8b_W T9b_W T10b_W ///
	F1bn_W F2bn_W F3bn_W F4bn_W F5bn_W F6bn_W F7bn_W F8bn_W F9bn_W F10bn_W ///
	T1bn_W T2bn_W T3bn_W T4bn_W T5bn_W T6bn_W T7bn_W T8bn_W T9bn_W T10bn_W ///
	, i(id) j(wave)

// Omit missing waves
drop if starttime_ == .
	
lab def w 1 "W1" 2 "W2" 3 "W3" 4 "W4" 5 "W5" 6 "W6" 7 "W7" 8 "W8" 9 "W9" ///
	10 "W10" 11 "W11" 12 "W12"
lab val wave w
xtset id wave, generic

//
// SDT calculations
//

// Missing values
recode F1b_W-T10bn_W (8/9 = .)

//
// How many statements believed
// "Definitely" or "Probably True" treated as belief
// NOTE: Strength of belief coding is reverse of typical rating tasks
// 	     That is, 1 is high confidence in "target," 4 is high in "lure"

// False, Familiar
egen ffnbel=anycount(F1b_W-F10b_W), value(1 2)
lab var ffnbel "Familiar falsehood belief count (T<=2)"
bysort wave: sum ffnbel

// True, Familiar
egen tfnbel=anycount(T1b_W-T10b_W), value(1 2)
lab var tfnbel "Familiar truth belief count (T<=2)"
bysort wave: sum tfnbel

// False, Unfamiliar
egen funbel=anycount(F1bn_W-F10bn_W), value(1 2)
lab var funbel "Unfamiliar falsehood belief count (T<=2)"
bysort wave: sum funbel

// True, Unfamiliar
egen tunbel=anycount(T1bn_W-T10bn_W), value(1 2)
lab var tunbel "Unfamiliar truth belief count (T<=2)"
bysort wave: sum tunbel 

// Totals
gen fnbel = ffnbel + funbel
lab var fnbel "Falsehood belief count (F<=2)"
gen tnbel = tfnbel + tunbel
lab var tnbel "Truth belief count (T<=2)"
bysort wave: sum fnbel tnbel

// c (Response bias)
// Negative average z score corresponding to hit rate and false-alarm rate 

// "Definitely" or "Probably True" treated as belief
gen H2 = tnbel/10
lab var H2 "Hit rate (T<=2)"
gen F2 = fnbel/10
lab var F2 "False-alarm rate (F<=2)"
bysort wave: sum H2 F2

// Recode extremes (H=0 and H=1) per standard practice

// "Definitely" or "Probably True" treated as belief
recode H2 (0 = .05) (1 = .95), gen(pH2)
recode F2 (0 = .05) (1 = .95), gen(pF2)
gen c2 = -(invnormal(pH2)+invnormal(pF2))/2
lab var c2 "c (T<=2)"
bysort wave: sum c2

// Adding statement slant data
// Note: id2 is sequential when waves converted to long (in id order)
gsort +id +wave
gen id2 = _n
merge 1:1 id2 using "tfSlantPublic.dta"
drop _merge id2

// Export a copy for density plots in R
export delimited using "panelBias.csv", replace nolabel

// Table S5
xtsum c2

//
// Response bias (c)
//

// *** Random effects regression models ***

// Table S7
xtreg c2 age maled educ feel evid TIP polid ///
	i.wave, re level(95)

// Fig. 2B
margins, at(polid=(1(1)7))
marginsplot, ///
	ytitle("Predicted response bias (c)") ylabel(-.5(.1)0) ///
	ysc(titlegap(-10)) ///
	xlabel(, labsize(small)) ///
	xtitle("Political ideology (conservatism high)", size(medium)) ///
	yline(0, lpattern("_#######") lw(thick) lc(black)) ///
	addplot( ///
	pcarrowi 0 7 -.25 7, lp(l) lw(thick) lc(black) mc(black) norescaling) ///
	text (-.1 7.15 "Truth bias", orientation(vertical) size(small)) ///
	ciopts(recast(rarea)) ///
	xsize(4.4) ysize(4.5) ///
	text(0.01 0 "{bf:B}", place(s)) ///
	title("") ///
	name(Fig2b, replace)
outreg2 using "S7", dec(3) alpha(0.001, 0.01, 0.05) word replace 		

// Table S9
xtreg c2 age maled educ feel evid TIP ///
	conT conF proT proF polid, re level(95)
outreg2 using "S9", dec(3) alpha(0.001, 0.01, 0.05) word replace 		
	
// *** Fixed effects regression models ***

// Table S11
xtreg c2 conT conF proT proF ///
	c.conT#c.polid c.conF#c.polid c.proT#c.polid c.proF#c.polid, ///
	fe level(95)
outreg2 using "S11", dec(3) alpha(0.001, 0.01, 0.05) word replace 		

// Fig. 5A
margins, at(conT=(.1(.1).9) polid=(1 7))
marginsplot, ///
	ytitle("Predicted response bias (c)") ylabel(-1.5(.5)1.5, labsize(small)) ///
	yline(0, lpattern("_#######") lw(thick) lc(black)) ///
	xlabel(0(.2)1, labsize(small)) text(1.5 .1 "{bf:A}", place(s)) ///
	xtitle("% truths that harm in-group", size(large)) ///
	plot1opts(lcolor(blue) mcolor(blue)) plot2opts(lcolor(red) mcolor(red)) ///
	ci1opts(lcolor(blue) recast(rarea)) ci2opts(lcolor(red) recast(rarea)) ///
	legend(order(3 "Very liberal" 4 "Very conservative")) ///
	xsize(4.4) ysize(4.5) ///
	title("") ///
	name(Fig5A, replace)

// Fig. 5B
margins, at(conF=(.1(.1).9) polid=(1 7))
marginsplot, ///
	ytitle("") ylabel(-1.5(.5)1.5, labsize(small)) ///
	yline(0, lpattern("_#######") lw(thick) lc(black)) ///
	xlabel(0(.2)1, labsize(small)) text(1.5 .1 "{bf:B}", place(s)) ///
	xtitle("% falsehoods that harm in-group", size(large)) ///
	addplot( ///
	pcarrowi 0 .95 1.4 .95, lp(l) lw(thick) lc(black) mc(black) || ///
	pcarrowi 0 .95 -1.4 .95, lp(l) lw(thick) lc(black) mc(black) norescaling) ///
	text (.75 .98 "Falsehood bias", orientation(vertical) size(vsmall)) ///
	text (-.75 .98 "Truth bias", orientation(vertical) size(vsmall)) ///
	plot1opts(lcolor(blue) mcolor(blue)) plot2opts(lcolor(red) mcolor(red)) ///
	ci1opts(lcolor(blue) recast(rarea)) ci2opts(lcolor(red) recast(rarea)) ///
	legend(order(3 "Very liberal" 4 "Very conservative")) ///
	xsize(4.4) ysize(4.5) ///
	title("") ///
	name(Fig5B, replace)

// Fig. 5C
margins, at(proT=(.1(.1).9) polid=(1 7))
marginsplot, ///
	ytitle("Predicted response bias (c)") ylabel(-1.5(.5)1.5, labsize(small)) ///
	yline(0, lpattern("_#######") lw(thick) lc(black)) ///
	xlabel(0(.2)1, labsize(small)) text(1.5 .1 "{bf:C}", place(s)) ///
	xtitle("% truths that benefit in-group", size(large)) ///
	plot1opts(lcolor(blue) mcolor(blue)) plot2opts(lcolor(red) mcolor(red)) ///
	ci1opts(lcolor(blue) recast(rarea)) ci2opts(lcolor(red) recast(rarea)) ///
	legend(order(3 "Very liberal" 4 "Very conservative")) ///
	xsize(4.4) ysize(4.5) ///
	title("") ///
	name(Fig5C, replace)

// Fig. 5D
margins, at(proF=(.1(.1).9) polid=(1 7))
marginsplot, ///
	ytitle("") ylabel(-1.5(.5)1.5, labsize(small)) ///
	yline(0, lpattern("_#######") lw(thick) lc(black)) ///
	xlabel(0(.2)1, labsize(small)) text(1.5 .1 "{bf:D}", place(s)) ///
	addplot( ///
	pcarrowi 0 .95 1.4 .95, lp(l) lw(thick) lc(black) mc(black) || ///
	pcarrowi 0 .95 -1.4 .95, lp(l) lw(thick) lc(black) mc(black) norescaling) ///
	text (.75 .98 "Falsehood bias", orientation(vertical) size(vsmall)) ///
	text (-.75 .98 "Truth bias", orientation(vertical) size(vsmall)) ///
	xtitle("% falsehoods that benefit in-group", size(large)) ///
	plot1opts(lcolor(blue) mcolor(blue)) plot2opts(lcolor(red) mcolor(red)) ///
	ci1opts(lcolor(blue) recast(rarea)) ci2opts(lcolor(red) recast(rarea)) ///
	legend(order(3 "Very liberal" 4 "Very conservative")) ///
	xsize(4.4) ysize(4.5) ///
	title("") ///
	name(Fig5D, replace)

// Fig. 5 (combined)
graph combine Fig5A Fig5B Fig5C Fig5D, row(2) ycommon ///
	name(Fig5,replace)
	
***
*** REPLICATE WITH Ps WHO COMPLETE 6+ or 12 waves
***

preserve
drop if completeWs<6

xtreg c2 age maled educ feel evid TIP polid ///
	i.wave, re level(95)

outreg2 using "S13", dec(3) alpha(0.001, 0.01, 0.05) word replace 		

xtreg c2 age maled educ feel evid TIP ///
	conT conF proT proF polid, re level(95)

outreg2 using "S15", dec(3) alpha(0.001, 0.01, 0.05) word replace 		

xtreg c2 conT conF proT proF ///
	c.conT#c.polid c.conF#c.polid c.proT#c.polid c.proF#c.polid, ///
	fe level(95)
	
outreg2 using "S17", dec(3) alpha(0.001, 0.01, 0.05) word replace 	
restore

preserve
drop if completeWs!=12

xtreg c2 age maled educ feel evid TIP polid ///
	i.wave, re level(95)

outreg2 using "S19", dec(3) alpha(0.001, 0.01, 0.05) word replace 		

xtreg c2 age maled educ feel evid TIP ///
	conT conF proT proF polid, re level(95)

outreg2 using "S21", dec(3) alpha(0.001, 0.01, 0.05) word replace 		

xtreg c2 conT conF proT proF ///
	c.conT#c.polid c.conF#c.polid c.proT#c.polid c.proF#c.polid, ///
	fe level(95)
	
outreg2 using "S23", dec(3) alpha(0.001, 0.01, 0.05) word replace 	
restore
